package com.qianguo.shop.fragment.tab;


import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.eva.androidlibrary.utils.IconfontUtil;
import com.qianguo.shop.R;
import com.qianguo.shop.activity.AddOrEditShopActivity;
import com.qianguo.shop.activity.ShopDetailActivity;
import com.eva.androidlibrary.api.HttpRequestCallback;
import com.qianguo.shop.activity.ShopLocationActivity;
import com.qianguo.shop.base.BaseFragment;
import com.eva.androidlibrary.api.entity.ShopInfo;
import com.eva.androidlibrary.api.entity.ShopListBean;
import com.eva.androidlibrary.api.entity.UserInfo;
import com.eva.androidlibrary.icon.FruitIcon;
import com.eva.androidlibrary.widget.SwipeRefreshRecyclerView;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 商家店铺fragment
 *
 * @author GUXIA318
 * @date 2015-09-14 17:27
 */
@EFragment(R.layout.fragment_tab_business_shop)
public class ShopTabFragment extends BaseFragment {

    @ViewById(R.id.shop_list_view)
    SwipeRefreshRecyclerView mShopListView;

    @ViewById(R.id.add_shop_text_view)
    TextView mTvAddShop;

    private int mCurrentPage = 1;
    private int mTotalNum;

    private List<ShopInfo> mShopItems = new ArrayList<>();
    private ShopCardAdapter mShopCardAdapter;

    static final int PER_PAGE_NUM = 10;


    @Click(R.id.add_shop_text_view)
    void clickAdd() {
        ShopLocationActivity.jumpToShopLocation(getActivity());
    }

    @Override
    protected void initializeUI() {
        IconfontUtil.setIcon(getActivity(), mTvAddShop, FruitIcon.ICON_ADD);
        mTvAddShop.append(getString(R.string.shop_add));

        mShopCardAdapter = new ShopCardAdapter(getActivity());
        mShopListView.setAdapter(mShopCardAdapter);

        mShopListView.setSwipeRefreshListener(new SwipeRefreshRecyclerView.SwipeRefreshListener() {
            @Override
            public void onLoadMore() {
                if (mShopItems.size() < mTotalNum) {
                    getShopInfo(++mCurrentPage);
                } else {
                    renderUI();
                }
            }

            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mShopItems.clear();
                getShopInfo(mCurrentPage);
            }
        });

        mShopListView.setOnItemClickListener(new SwipeRefreshRecyclerView.OnRecyclerItemClickListener() {
            @Override
            public void onClick(int position) {
                ShopDetailActivity.jumpToShopDetail(getActivity(), mShopItems.get(position));
            }
        });

        getShopInfo(mCurrentPage);
    }

    @Background
    void getShopInfo(int page) {
        UserInfo user = mApp.getUser();
        mApp.getApi().getShopList(user.getToken(), page, PER_PAGE_NUM, new HttpRequestCallback<ShopListBean>() {
            @Override
            public void onSuccess(int code, String msg, ShopListBean data) {
                if (data != null) {
                    if (mTotalNum == 0) {
                        mTotalNum = data.getTotal();
                    }
                    List<ShopInfo> list = data.getList();
                    if (list != null) {
                        mShopItems.addAll(list);
                    }
                    renderUI();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    @UiThread(propagation = UiThread.Propagation.REUSE)
    void renderUI() {
        mShopListView.setIsLoadMore(mShopItems.size() != mTotalNum);
        mShopListView.onCompleted();
    }

    private class ShopCardAdapter extends RecyclerView.Adapter<ShopCardViewHolder> {

        private Context mContext;
        public ShopCardAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public ShopCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_shop_list, null);
//            v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new ShopCardViewHolder(v);
        }

        @Override
        public void onBindViewHolder(ShopCardViewHolder holder, int position) {
            ShopInfo item = mShopItems.get(position);
            mImageLoader.loadImage(mContext, holder.imgCover, item.getLogo());
            holder.tvName.setText(item.getName());
            holder.tvAddress.setText(item.getAddress());
//            holder.tvOpenHours.setText();
        }

        @Override
        public int getItemCount() {
            return mShopItems != null ? mShopItems.size() : 0;
        }
    }

    private class ShopCardViewHolder extends RecyclerView.ViewHolder {

        ImageView imgCover;
        TextView tvName;
        TextView tvAddress;
        TextView tvOpenHours;

        public ShopCardViewHolder(View itemView) {
            super(itemView);
            imgCover = (ImageView) itemView.findViewById(R.id.shop_image_view);
            tvName = (TextView) itemView.findViewById(R.id.shop_name_text_view);
            tvAddress = (TextView) itemView.findViewById(R.id.shop_location_text_view);
            tvOpenHours = (TextView) itemView.findViewById(R.id.shop_opening_hours_text_view);
        }
    }
}
